﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MutilLanguage_Search.Connection;
using MutilLanguage_Search.Controller;
using MutilLanguage_Search.Controller.GetLinks;
using MutilLanguage_Search.Controller.GetKeyWords;
using MutilLanguage_Search.Controller.GetTopLinks;
using System.Diagnostics;
namespace MutilLanguage_Search
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Data.Init.init();
            SourceAnalysis.Synonym = lbxSyn;
            SourceAnalysis.Translate = lbxTra;
            GetLinksSynonyms.lbxLinks = lbxLinkSyn;
            GetKeyWordsSyn.lbxTopKeyWordsSyn = lbxS_Keywords;
            GetTopLinksSynonyms.lblTopLinksSynonym = lbxTopLinkSyn;
            Define.Variables.st = new Stopwatch();
        }
        public void Init()
        {
            Define.Variables.contentSyn = "";
            Define.Variables.countSyn = 0;
            Define.Variables.countLinkSyn = 0;
            Data.Init.sdSynonyms.Clear();
            Data.Init.sdTranslate.Clear();
            Data.Init.sdLinksSyn.Clear();
            Data.Init.sdLinksTra.Clear();
            Data.Init.sdKeyWordsSyn.Clear();
            Data.Init.sdKeyWordsTra.Clear();
            Data.Init.sdKTopeyWordsTra.Clear();
            Data.Init.sdTopKeyWordsSyn.Clear();
            lbxSyn.Items.Clear();
            lbxTra.Items.Clear();
            lbxLinkSyn.Items.Clear();
            lbxLinkTra.Items.Clear();
            lbxS_Keywords.Items.Clear();
            lbxT_Keywords.Items.Clear();
            lbxTopLinkSyn.Items.Clear();
            lbxTopLinkTra.Items.Clear();
            Define.Variables.st.Start();
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {

            Init();
            string source = "";
            if (rbEV.Checked)
            {
                source = Connection.Web.RequestVdictEN(tbxSearch.Text.Trim());
                if(!source.Equals("ERROR"))
                {
                    SourceAnalysis.ParseVdictEN(source, tbxSearch.Text.Trim());
                    setLinks();
                }
            }
            else
            {
                source = Connection.Web.RequestVdictVN(tbxSearch.Text.Trim());
            }
        }

        private void setLinks()
        {
            if (Data.Init.sdSynonyms.Count != 0)
            {
                setLinksSynonyms();
            }
        }

        private void setLinksSynonyms()
        {
            foreach (KeyValuePair<string, int> kv in Data.Init.sdSynonyms)
            {
                GetLinksSynonyms.Init(kv.Key);
            }
        }
    }
}
